/*
Copyright 2010 Christian Dadswell
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package subscriber.chrisdadswell.co.uk;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashMap;

public class Func_HTTP {

	public static HashMap<String, String> _FEED_HASHMAP = new HashMap();
	public static final String _APP_TAG = "Func_HTTP";

	public static String GetHeadContent(String SiteURL) throws IOException {
	      String _WEBCONTENT = "";
	      String _HEADCONTENT = "";
	      
	      URL url = new URL(SiteURL);
	      BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

	      while ((_WEBCONTENT = in.readLine()) !=null) {
	        _HEADCONTENT += _WEBCONTENT + "\n";
	        if(Func_Strings.FindWordInString(_WEBCONTENT, "</head>")) {
	          return _HEADCONTENT;
	        }
	        }
	      in.close();
	    return "";
	    }
	
	public static boolean FindFeeds(final String URL) {
		HashMap<String, String> _FEED_HASHMAP = new HashMap();
    	try {
    		_FEED_HASHMAP = Func_JSOUP.getAvailableFeeds(URL);
    	} catch (IOException e) {
    		e.printStackTrace();
    	}
    	
    	if(!_FEED_HASHMAP.isEmpty()){
    		SetFeedHash(_FEED_HASHMAP);
    		return true;
    	}else{
    		return false;
    	}
    }
	
	private static void SetFeedHash(HashMap ReturnedFeedsHash) {
    	_FEED_HASHMAP = ReturnedFeedsHash;
    }
    
// GETS THE TITLES OF THE FEEDS FROM A HASHMAP
    public static String[] GetFeedHashKeys() {
    	String[] _FEED_TITLES = new String[_FEED_HASHMAP.size()]; 
    	_FEED_HASHMAP.keySet().toArray(_FEED_TITLES);
        return _FEED_TITLES;
    }
    
// GETS THE FEED URL FOR A GIVEN FEED TITLE (KEY) 
    public static final String GetFeedHashValue(String FeedToFind) {
    	String _TMP = null;;
    	String _FEED_URL = null;
    	
    	_TMP = _FEED_HASHMAP.get(FeedToFind).toString();
    	_FEED_URL = _TMP;
    	return _FEED_URL;
    }
}